module dwt.util.javatypes;

private import dwt.dwt;
private import dwt.util.util;


/**
 * A tagging interface that all event listener interfaces must extend.
 * @since JDK1.1
 */
public interface EventListener {
}



/**
 * The Integer class wraps a value of the primitive type int in an object. 
 * An object of type Integer contains a single field whose type is int. 
 */
 
public class Integer {
	private import Int = tango.text.convert.Integer ;

	
/**
 * A constant holding the minimum value an <code>int</code> can
 * have, -2<sup>31</sup>.
 */
public static final int   MIN_VALUE = 0x80000000;

/**
 * A constant holding the maximum value an <code>int</code> can
 * have, 2<sup>31</sup>-1.
 */
public static final int   MAX_VALUE = 0x7fffffff;
public int value;

alias Int.parse!(char) parseInt;


public static int parseInt(char[] s, int radix){
//		// TODO:
//		Util.trace(__FILE__, __LINE__, ", Not implemented yet");
//		return 0;

    if (s is null) {
        throw new Exception("null");
    }

	if (radix < 2) {
	    throw new Exception("radix less than Character.MIN_RADIX");
	}

	if (radix > 36) {
	    throw new Exception("radix greater than Character.MAX_RADIX");
	}

	int result = 0;
	boolean negative = false;
	int i = 0, max = s.length;
	int limit;
	int multmin;
	int digit;

	if (max > 0) {
	    if (s[0] == '-') {
		negative = true;
		limit = Integer.MIN_VALUE;
		i++;
	    } else {
		limit = -Integer.MAX_VALUE;
	    }
	    multmin = limit / radix;
	    if (i < max) {
		digit = Character.digit(s[i++], radix);
		if (digit < 0) {
		    throw new Exception(("For input string: \"") ~ s ~ "\"");
		} else {
		    result = -digit;
		}
	    }
	    while (i < max) {
		// Accumulating negatively avoids surprises near MAX_VALUE
		digit = Character.digit(s[i++],radix);
		if (digit < 0) {
		    throw new Exception(("For input string: \"") ~ s ~ "\"");
		}
		if (result < multmin) {
		    throw new Exception(("For input string: \"") ~ s ~ "\"");
		}
		result *= radix;
		if (result < limit + digit) {
		    throw new Exception(("For input string: \"") ~ s ~ "\"");
		}
		result -= digit;
	    }
	} else {
	    throw new Exception(("For input string: \"") ~ s ~ "\"");
	}
	if (negative) {
	    if (i > 1) {
		return result;
	    } else {	/* Only got "-" */
		throw new Exception(("For input string: \"") ~ s ~ "\"");
	    }
	} else {
	    return -result;
	}

}

public alias Int.toString toString;


public this(int value) {
	this.value = value;
}

public boolean equals(Object obj) {
if (cast(Integer)obj) {
    return value == (cast(Integer)obj).intValue();
}
return false;
}

//alias value intValue;
public int intValue() {return value;}
public void intValue(int val) { value = val; }
public int opEquals(Integer obj) {
	return this.equals(obj);
}
public int hashCode() {	return value;    }
public uint toHash() {	return cast(uint)value;    }
}


/**
The Long class wraps a value of the primitive type long in an object. 
An object of type Long contains a single field whose type is long. 
*/

class Long {
public long value;
public this(long value) {
	this.value = value;
}

public boolean equals(Object obj) {
if (cast(Long)obj) {
    return value == (cast(Long)obj).longValue();
}
return false;
}

public long longValue() {return value;}
public void longValue(long val) { value = val;}
public int opEquals(Integer obj) {
	return this.equals(obj);
}
public int hashCode() {	return cast(int)(value ^ (value >> 32)); }
public uint toHash() {return cast(uint)(value ^ (value >> 32)); }
}

class Character {
	
	public static int digit(int codePoint, int radix){
		Util.trace((__FILE__) , ", " , __LINE__ , "Not implemented : Character.digit()");
//		assert(0);
		return 0;
	}
	
	public static char toUpperCase (char c) {
		if ('a' <= c && c <= 'z') {
			return c - (cast(char)'a' - 'A');
		} else {
			return c;
		}
	}
	
	public static boolean isUpperCase(char c){
		return ('A' <= c && c <= 'Z');
	}

	public static char toLowerCase (char c) {
		if ('A' <= c && c <= 'Z') {
			return c + (cast(char)'a' - 'A');
		} else {
			return c;
		}
	}
	public static boolean isLowerCase(char c){
		return ('a' <= c && c <= 'z');
	}

	public static boolean isDigit(char c) {
		return ( '0' <= c && c <= '9');
	}
}

interface Runnable {
    void run();
}

// it is better let the user define his own class
deprecated class FunctionRunner : Runnable {
	void function() func;
	this(void function() f) {
		func = f;
	}

	void run() {
		func();
	}
}

// it is better let the user define his own class
deprecated class DelegateRunner : Runnable {
	void delegate() dlg;
	this(void delegate() d) {
		dlg = d;
	}

	void run() {
		dlg();
	}
}

